<?php

namespace App\Admin\Renderables\DyStatistic;

use App\Models\DdbCbrx;
use App\Models\DyBaseInfo;
use Dcat\Admin\Support\LazyRenderable;
use Dcat\Admin\Widgets\Table;
use Illuminate\Support\Facades\DB;

class StatisticForDyBaseInfoRenderTable extends LazyRenderable
{
    public function render()
    {
        $modelClass = $this->payload['modelClass'];

        $counts = $modelClass::query()->select(DB::raw('dwmc, dwdm, count(*) as count'))->groupBy('dwdm')->get()->toArray();
        $counts = collect($counts)->keyBy('dwdm')->all();

        $headers = ['院级党组织名称', '登记人数'];
        $rows = [];
        foreach(dzzs() as $dwdm => $dwmc) {
            $count = isset($counts[$dwdm]) ? $counts[$dwdm]['count'] : 0;
            if($count == 0){
                $dwmc = '<span class="text-danger">'.$dwmc.'</span>';
            }
            array_push($rows, [
                $dwmc,
                $count
            ]);
        }
        $table = Table::make($headers, $rows)->withBorder()->render();
        return $table;
    }
}